<?php
/**
 * gd mobile theme model
 *
 * @copyright        2017 opencart.cn - All Rights Reserved
 * @link             http://www.guangdawangluo.com
 * @author           Sam Chen <sam.chen@opencart.cn>
 * @created          2017-08-07 12:21:10
 * @modified         2017-08-07 14:04:48
 */

class ModelExtensionThemeGdMobile extends Model {
  private $code = 'gd_mobile';

  public function install() {
    $file = DIR_APPLICATION . 'model/extension/theme/' . $this->code . '.sql';

    if (!file_exists($file)) {
      return;
    }

    $lines = file($file);

    if (!$lines) {
      return;
    }

    $sql = '';
    foreach ($lines as $line) {
      if ($line && (substr($line, 0, 2) != '--') && (substr($line, 0, 1) != '#')) {
        $sql .= $line;

        if (preg_match('/;\s*$/', $line)) {
          $sql = str_replace("DROP TABLE IF EXISTS `oc_", "DROP TABLE IF EXISTS `" . DB_PREFIX, $sql);
          $sql = str_replace("CREATE TABLE `oc_", "CREATE TABLE `" . DB_PREFIX, $sql);
          $sql = str_replace("INSERT INTO `oc_", "INSERT INTO `" . DB_PREFIX, $sql);

          $this->db->query($sql);

          $sql = '';
        }
      }
    }
  }
}
